#!binbash
ip_mediator1=10.47.61.114
IP=ip_mediator1
remote_host=admin@${!IP}

#agent
AGENT_DIR=mytijianagent
HOSPITAL_DIR=${AGENT_DIR}hospitals
TEMP_DIR=tmpagent_tmp
PROBE_TEMP=tmpprobe_tmp

#webservice
temp_dir=tmpweb_agent
agent_dir=mytijianagentclientwebservice
hospital_dir=mytijianagenthospitals

tmpalert
tmpalert_probe
tmpalert_zip
tmpalert_git

#切换分支
[ $ != 0 ]&& exit 1
git checkout develop
git pull origin develop


#遍历医院
for i in $hospital
do
    cd $WORKSPACEhospital${i}
    #判断医院编号
    if [ $ != 0 ];then
        echo [$i] id errorbrtmpalert
        continue
    fi
    
    echo =====创建${i}的agent包目录=====
    ssh ${remote_host} [ ! -d $HOSPITAL_DIR$i ]&& mkdir -p $HOSPITAL_DIR$i
    
    isWebservice=`cat config.properties  grep -E MytijianGenericPEService`
    #echo $isWebservice
    
    if [ $isWebservice !=  ];then
    	#webservices
    	echo ====================webservices=========================
        if [ -d ${WORKSPACE}MediatorAgentConfig ];then
        	rm -rf ${WORKSPACE}MediatorAgentConfig
        fi
        
        cd ${WORKSPACE}
        git clone -b develop git@gitee.commytjMediatorAgentConfig.git
        #[ $ != 0 ]&&exit 1
        
        #清理临时目录
        ssh  ${remote_host} [ -d $temp_dir ]
    
        if [ $ = 0 ];then
            ssh  ${remote_host} rm -rf $temp_dir
        else
            ssh  ${remote_host} mkdir -p ${temp_dir} #tmpweb_tmp
            if [ $ != 0 ];then
                echo create ${temp_dir} failedtmpalert
                exit 1
            fi
        fi
        
        #解压war包到临时目录
        ssh ${remote_host} mkdir -p ${temp_dir}mediator-agent && unzip $agent_dir$versionclient-agent-$version.war -d $temp_dirmediator-agent
        if [ $ -eq 0 ];then
        	echo 成功
        fi
        #替换配置文件
        scp $WORKSPACEMediatorAgentConfighospital${i}config.properties ${remote_host}${temp_dir}mediator-agentWEB-INFclasses
        
        #移动agent包到医院目录
        ssh ${remote_host} cd $temp_dir && zip -r mediator-agent.zip  && mv mediator-agent.zip $hospital_dir$i
        #ssh ${remote_host} cd $temp_dir && rm -rf mediator-agent
        echo +++++打包结束+++++
        
    else 
    	
        #清理临时目录
        ssh  ${remote_host} [ -d $TEMP_DIR ]
        
        if [ $ = 0 ];then
            ssh  ${remote_host} rm -rf $TEMP_DIR
        else
            ssh  ${remote_host} mkdir -p ${TEMP_DIR}
            if [ $ != 0 ];then
                echo create ${TEMP_DIR} failedtmpalert
                exit 1
            fi
        fi

        #解压war包到临时目录
        ssh ${remote_host} mkdir -p $TEMP_DIR{mediator-agent,control} &&
            unzip $AGENT_DIRclient$versionmediator-agent-${version}.war -d $TEMP_DIRmediator-agent &&
            unzip $AGENT_DIRclient$versioncontrol-${version}.war -d $TEMP_DIRcontrol
        
        
        function delete_proxy_config(){
            if [ -f cxf-proxy.xml ];then
                echo =====删除临时目录${i}代理配置文件=====
                ssh ${remote_host} rm -f $TEMP_DIRmediator-agentWEB-INFclassescxf-proxy.xml
                ssh ${remote_host} rm -f $TEMP_DIRcontrolWEB-INFclassescxf-proxy.xml
                ssh ${remote_host} rm -f $TEMP_DIRmediator-agentWEB-INFclassesapplicationContext.xml
                ssh ${remote_host} rm -f $TEMP_DIRcontrolWEB-INFclassesapplicationContext.xml
            fi
        
        }
    	echo =====更新${i}的configwget.sh配置文件=====
    	sed -i s[0-9]d.d[0-9].d[0-9]$versiong configwget.sh
    	echo ${i} alert versiontmpalert_git

    	if [ -f cxf-proxy.xml ];then
	    	#代理
	    	echo ======由于使用代理,拷贝${i}代理文件到临时目录======
	        scp cxf-proxy.xml ${remote_host}$TEMP_DIRmediator-agentWEB-INFclassescxf-proxy.xml
	        scp cxf-proxy.xml ${remote_host}$TEMP_DIRcontrolWEB-INFclassescxf-proxy.xml
	        scp applicationContext.xml ${remote_host}$TEMP_DIRmediator-agentWEB-INFclassesapplicationContext.xml
	        scp control-applicationContext.xml ${remote_host}$TEMP_DIRcontrolWEB-INFclassesapplicationContext.xml
	    fi

	    #更换configwget.sh配置文件
		echo =====更新${i}的configwget.sh配置文件=====
    	sed -i s[0-9]d.d[0-9].d[0-9]$versiong configwget.sh
    	echo ${i} alert versiontmpalert_git

	    echo ======拷贝${i}配置文件到临时目录======
	    scp agent-db.properties ${remote_host}$TEMP_DIRmediator-agentWEB-INFclassesagent-db.properties
	    scp config.properties ${remote_host}$TEMP_DIRmediator-agentWEB-INFclassesconfig.properties
	    #scp tasks.properties ${remote_host}$TEMP_DIRmediator-agentWEB-INFclassestasks.properties
	    scp control.config.properties ${remote_host}$TEMP_DIRcontrolWEB-INFclassesconfig.properties
	    scp hisdbip.txt ${remote_host}$TEMP_DIRmediator-agentWEB-INFclasseshisdbip.txt

	    echo =====判断${i}的probe是否有值=====
	    PROBE=`grep 'mediator_probe' configwget.sh sed 's(.)(.)(.)2g'`
	    if [ -z $PROBE ]; then
	        echo ${i} probe errorbrtmpalert_probe
			#delete_proxy_config
	        continue
	    else
	        ssh ${remote_host} unzip -o $AGENT_DIRclient$versionprobes$PROBE -d $TEMP_DIRmediator-agentWEB-INFlib &&
	        cd $TEMP_DIR && zip -r mediator-agent.zip  && mv mediator-agent.zip $HOSPITAL_DIR$i
	        if [ $ != 0 ]; then
	            echo ${i} failedbrtmpalert_zip
				#delete_proxy_config
	            continue
	        else
	            echo ${i} sussessbrtmpalert
	        fi
	    fi

	    echo =====删除probe中jar包=====
	    ssh ${remote_host} unzip -t $AGENT_DIRclient$versionprobes$PROBE grep 'testing'awk  '{print $2}' tmp.tmp_probe
	    for jarfile in `ssh ${remote_host} cat tmp.tmp_probe`
	    do
	        ssh ${remote_host} find $TEMP_DIRmediator-agentWEB-INFlib -type f -name $jarfile -exec rm -f {} ;
	    done

	    if [ -f cxf-proxy.xml ];then
	        echo =====删除临时目录${i}代理配置文件=====
	        ssh ${remote_host} rm -f $TEMP_DIRmediator-agentWEB-INFclassescxf-proxy.xml
	        ssh ${remote_host} rm -f $TEMP_DIRcontrolWEB-INFclassescxf-proxy.xml
	        ssh ${remote_host} rm -f $TEMP_DIRmediator-agentWEB-INFclassesapplicationContext.xml
	        ssh ${remote_host} rm -f $TEMP_DIRcontrolWEB-INFclassesapplicationContext.xml
	    fi
	fi
done

echo =====提交代码====
git config --global user.email cwshi@mytijian.com
git config --global user.name cwshi

cd $WORKSPACE
git add 
str_commit=`cat tmpalert_git`
git commit -m $str_commit
git push origin develop

echo ======end======